<?php

class Sistema_InteresseController extends App_Controller_Action
{

    private $_table     = 'INTERESSE';
    private $_pk        = 'INT_ID';
    private $_order_col = 'INT_DAT';
    private $_order     = 'DESC';
    private $_sta_col   = 'INT_STA';
    private $obj        = '';
    private $form       = '';
    private $_file_col  = 'INT_FILE'; 
    public  $dir = 'public/upload//';
    public  $db = '';
    
    public function init()
    {
        //$this->obj = new Sistema_Model_DbTable_Interesse();
        //if($this->getRequest()->getActionName() == 'edit') $this->form = new Sistema_Form_Interesse();
        $this->db = Zend_Db_Table_Abstract::getDefaultAdapter();
        
    }

    public function indexAction() {
        
        Zend_Loader::loadClass('tools','library');
        Zend_Loader::loadClass('datetools','library');
        Zend_Loader::loadClass('dominio','library');
        
        $where = 'WHERE '.$this->_pk . ' > 0';
        
        //$STA = $this->_getParam($this->_sta_col, 1);
        //$where .= ' and ' . $this->_sta_col . ' = ' . $STA;
        
        //$result = $this->obj->fetchAll($this->obj->select()->where($where)->order($this->_order_col.' '.$this->_order));
        
        $query = 'SELECT SQL_CACHE i.*, c.CBS_NOM FROM '.$this->_table.' i, CURSOBASE c '.$where.' and i.CBS_ID = c.CBS_ID ORDER BY '.$this->_order_col.' '.$this->_order;
        $result = $this->db->fetchAll($query);
        $this->view->result = $this->paginacao($result);
        
    }
    
    public function editAction() {
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/validation/dist/jquery.validate.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_geral.js');
        
        Zend_Loader::loadClass('imagem','library');
        Zend_Loader::loadClass('datetools','library');
        Zend_Loader::loadClass('dominio','library');
        
        //$this->view->view = 'view';
        
        if($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            try {
                $id = (int) $formData[$this->_pk];

                $data = $formData;

                unset($data['img']);
                unset($data[$this->_file_col]);
                unset($data['submit']);
                unset($data['CBS_NOM']);
                
                $data['INT_DAT'] = Datetools::dma_amd($data['INT_DAT']);

                if($id == 0){
                    $this->db->insert($this->_table, $data);
                    $id =  $this->db->lastInsertId();   
                }
                else $this->db->update($this->_table, $data,$this->_pk.' = ' . $id);

                $this->_helper->redirector('index');                
            } catch(Exception $ex) {
                Zend_Loader::loadClass('errordb','library');
                echo '<script>alert("'.$ex->getMessage().'");</script>';
                $this->view->data = $formData;
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                //$data = $this->obj->fetchRow($this->_pk . ' = ' . $id)->toArray();
                $data = $this->db->fetchRow('SELECT SQL_CACHE * FROM '.$this->_table.' WHERE '.$this->_pk . ' = ' . $id);
                $data['INT_DAT'] = Datetools::amd_dma($data['INT_DAT']);
                $this->view->data = $data;
                
                $curso = $this->db->fetchRow('SELECT SQL_CACHE * FROM CURSOBASE WHERE CBS_ID = '.$data['CBS_ID'].' and CBS_STA = 1');
                $this->view->curso = $curso;
                
                $query = 'SELECT SQL_CACHE n.* FROM INTERESSE_HAS_HORARIO i, NUMEROTURMA n WHERE '.$this->_pk . ' = ' . $id. ' and i.NRT_ID = n.NRT_ID ORDER BY n.NRT_SEM ASC, n.NRT_HOR ASC';
                $horarios = $this->db->fetchAll($query);
                $this->view->horarios = $horarios;
            }
        }
    }

    public function deleteAction() {
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        $ok = FALSE;
        $id = $this->_getParam('id', 0);
        if ((int) $id > 0 ) {   
            //$data = array($this->_sta_col => 0);
            //$ok = (bool) $this->db->update($this->_table,$data, $this->_pk . ' = ' . $id);
            $ok = (bool) $this->db->delete('INTERESSE_HAS_HORARIO', $this->_pk . ' = ' . $id);
            $ok = (bool) $this->db->delete($this->_table, $this->_pk . ' = ' . $id);
        }      
        echo json_encode(array('ok' => $ok));
        //$this->_helper->redirector('index');
    }

}